package com.wdf.dota;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class ViewFile extends Activity {
	private String filenameString;
	public static final String VIEWFILE_ACTION = "com.wdf.dota.VIEWFILE_ACTION";
	private static final String defaultCode = "gb2312";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filebrowser);
		try {
			Bundle bunde = this.getIntent().getExtras();
			filenameString = bunde.getString("name");
			refreshGUI(defaultCode);
		} catch (Exception e) {
		}
	

	}

	private void refreshGUI(String code)
	{
		TextView tv = (TextView) findViewById(R.id.view_contents);
		String fileContent = getStringFromFile(code);
		tv.setText(fileContent);
	}
	
	public String getStringFromFile(String code)
	{
		try {
			StringBuffer sBuffer = new StringBuffer();
			InputStream fis = getAssets().open( filenameString+".txt");
			//FileInputStream fInputStream = (FileInputStream)fis;
			InputStreamReader inputStreamReader = new InputStreamReader(fis, code);
			BufferedReader in = new BufferedReader(inputStreamReader);
			while (in.ready()) {
				sBuffer.append(in.readLine() + "\n");
			}
			in.close();
			return sBuffer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	// ��ȡ�ļ�����
	public byte[] readFile(String fileName) throws Exception {
		byte[] result = null;
		FileInputStream fis = null;
		try {
			File file = new File(fileName);
			fis = new FileInputStream(file);
			result = new byte[fis.available()];
			fis.read(result);
		} catch (Exception e) {
		} finally {
			fis.close();
		}
		return result;
	}

}
